﻿
namespace EveKillboard.Services.Feeds.CcpApi
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using Core.Base;
    using Core.Services;


    public sealed class CorporationKillmailFeedProvider : HttpKillmailFeedProviderBase
    {
        public CorporationKillmailFeedProvider(string url, Dictionary<int, string> keys) 
            : base(url, keys){ }

        public override IEnumerable<IFeedKillmail> GetKillmails()
        {
            var result = new List<IFeedKillmail>();
            if (string.IsNullOrEmpty(Url) || Keys == null || !Keys.Any()) return result;

            try
            {
                var parser = new Core.Parsers.XmlFeedParser();
                
                using(var enu = Keys.GetEnumerator())
                    while (enu.MoveNext())
                    {
                        if(enu.Current.Key == 0 || string.IsNullOrEmpty(enu.Current.Value)) continue;

                        var request = CreateRequest(string.Format("{0}?keyID={1}&vCode={2}", Url, enu.Current.Key,enu.Current.Value));
                        var response = request.GetResponse();
                        var st = response.GetResponseStream();
                        if(st!=null)
                            using(st)
                            {
                                try
                                {
                                    parser.Parse(st);
                                    if (parser.HasError && parser.ErrorCode != ERROR_NOKILL)
                                    {
                                        throw new Exception(string.Format("Code: {0}. Message: {1}", parser.ErrorCode, parser.ErrorMessage));
                                    }
                                }
                                catch (Exception )
                                {
                                }

                                if(parser.Killmails !=null && parser.Killmails.Any())
                                    result.AddRange(parser.Killmails);
                            }

                        response.Close();
                    }

                parser = null;
            }
            catch (Exception )
            {
            }

            return result;
        }
    }

    public sealed class CharacterKillmailFeedProvider : HttpKillmailFeedProviderBase
    {
        public CharacterKillmailFeedProvider(string url, Dictionary<int, string> keys) 
            : base(url, keys){ }
        
        public override IEnumerable<IFeedKillmail> GetKillmails()
        {
            var result = new List<IFeedKillmail>();
            if (string.IsNullOrEmpty(Url) || Keys == null || !Keys.Any()) return result;
            try
            {
                var parser = new Core.Parsers.XmlFeedParser();

                using (var enu = Keys.GetEnumerator())
                    while (enu.MoveNext())
                    {
                        if (enu.Current.Key == 0 || string.IsNullOrEmpty(enu.Current.Value)) continue;

                        var request = CreateRequest(string.Format("{0}?keyID={1}&vCode={2}", Url, enu.Current.Key, enu.Current.Value));
                        var response = request.GetResponse();
                        var st = response.GetResponseStream();
                        if (st != null)
                            using (st)
                            {
                                try
                                {
                                    parser.Parse(st);
                                    if (parser.HasError && parser.ErrorCode != ERROR_NOKILL)
                                    {
                                        throw new Exception(string.Format("Code: {0}. Message: {1}", parser.ErrorCode, parser.ErrorMessage));
                                    }
                                }
                                catch (Exception )
                                {
                                }

                                if (parser.Killmails != null && parser.Killmails.Any())
                                    result.AddRange(parser.Killmails);
                            }

                        response.Close();
                    }

                parser = null;
            }
            catch (Exception )
            {
            }

            return result;
        }
    }
}